﻿@namespace ThingsGateway.Gateway.Razor
@using System.Text.Json.Nodes
@using Microsoft.Extensions.Hosting
@using ThingsGateway.Admin.Application
@using ThingsGateway.Admin.Razor
@using ThingsGateway.Gateway.Application
@attribute [JSModuleAutoLoader("Pages/GatewayMonitorPage/Variable/VariableRuntimeInfo.razor.js", AutoInvokeInit = true, AutoInvokeDispose = false, JSObjectReference = true)]
@inherits ThingsGatewayModuleComponentBase

@* RenderMode="TableRenderMode.Table"
      ShowCardView=false *@

<AdminTable Id=@Id @ref=table BeforeShowEditDialogCallback="BeforeShowEditDialogCallback"
            TItem="VariableRuntime"
            RenderMode="TableRenderMode.Table"
            ShowCardView=false
            EditDialogSize="Size.ExtraLarge"
            AutoGenerateColumns="true"
            ShowAdvancedSearch=false
            ScrollingDialogContent=false
            AllowResizing="true"
            OnAdd="OnAdd"
            IsFixedHeader=true
            IsMultipleSelect=true
            SearchMode=SearchMode.Top
            ShowExtendButtons=true
            ShowToolbar="true"
            ShowExportButton
            OnColumnCreating="OnColumnCreating"
            OnColumnVisibleChanged=OnColumnVisibleChanged
            ShowDefaultButtons=true
            ShowSearch=false
            ExtendButtonColumnWidth=220
            OnSaveAsync="Save"
            OnDeleteAsync="Delete"
            OnQueryAsync="OnQueryAsync"
            IsPagination=true>
    <TableColumns>
        <TableColumn Field="@context.DeviceId" FieldExpression=@(() => context.DeviceId) ShowTips=true Filterable=true Sortable=true Visible=false />
        <TableColumn Field="@context.DeviceName" FieldExpression=@(() => context.DeviceName) ShowTips=true Filterable=true Sortable=true Visible=true />
        <TableColumn @bind-Field="@context.Name" ShowTips=true Filterable=true Sortable=true Visible=true />
        <TableColumn @bind-Field="@context.Description" ShowTips=true Filterable=true Sortable=true Visible=true />
        <TableColumn @bind-Field="@context.CollectGroup" ShowTips=true Filterable=true Sortable=true Visible=false />
        <TableColumn @bind-Field="@context.BusinessGroup" ShowTips=true Filterable=true Sortable=true Visible=false />
        <TableColumn @bind-Field="@context.BusinessGroupUpdateTrigger" ShowTips=true Filterable=true Sortable=true Visible=false />
        <TableColumn @bind-Field="@context.RpcWriteCheck" ShowTips=true Filterable=true Sortable=true Visible=false />

        <TableColumn @bind-Field="@context.Enable" Filterable=true Sortable=true Visible="false" />
        <TableColumn Field="@context.ChangeTime" Width=120 ShowTips=true FieldExpression=@(() => context.ChangeTime) Filterable=true Sortable=true Visible=false />
        <TableColumn Field="@context.CollectTime" Width=120 ShowTips=true FieldExpression=@(() => context.CollectTime) Filterable=true Sortable=true Visible=true />
        <TableColumn Field="@context.IsOnline" FieldExpression=@(() => context.IsOnline) Filterable=true Sortable=true Visible=true />
        <TableColumn Field="@context.LastErrorMessage" ShowTips=true FieldExpression=@(() => context.LastErrorMessage) Filterable=true Sortable=true Visible=false />

        <TableColumn Field="@context.LastSetValue" FieldExpression=@(() => context.LastSetValue) Visible=false ShowTips=true Formatter=@(JsonFormatter) />

        <TableColumn Field="@context.RawValue" FieldExpression=@(() => context.RawValue) Visible=false ShowTips=true Formatter=@(JsonFormatter) />
        <TableColumn Field="@context.Value" FieldExpression=@(() => context.Value) Visible=true ShowTips=true Formatter=@(JsonFormatter) />

        <TableColumn @bind-Field="@context.SaveValue" Filterable=true Sortable=true Visible=false />
        <TableColumn @bind-Field="@context.DataType" Filterable=true Sortable=true Visible=false />
        <TableColumn Field="@context.RuntimeType" ShowTips=true FieldExpression=@(() => context.RuntimeType) Filterable=true Sortable=true Visible=false />

        <TableColumn @bind-Field="@context.RegisterAddress" Filterable=true Sortable=true Visible=true />
        <TableColumn @bind-Field="@context.ArrayLength" Filterable=true Sortable=true Visible=false />

        <TableColumn @bind-Field="@context.ProtectType" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.IntervalTime" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.OtherMethod" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.RpcWriteEnable" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.ReadExpressions" Filterable=true Sortable=true Visible="true" />
        <TableColumn @bind-Field="@context.WriteExpressions" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.Unit" Filterable=true Sortable=true Visible="false" />

        <TableColumn @bind-Field="@context.Index" Filterable=true Sortable=true Visible="false" />
        <TableColumn @bind-Field="@context.Id" Filterable=true Sortable=true Visible="false" DefaultSort=true DefaultSortOrder="SortOrder.Asc" />


    </TableColumns>
    <RowButtonTemplate>

        <PopConfirmButton IsKeepDisabled=@(!AuthorizeButton("写入变量")) Size="Size.ExtraSmall" Color="Color.Warning" Icon="fa-solid fa-bars" Text="@Localizer["WriteVariable"]" IsAsync OnConfirm="() => OnWriteVariable(context)">

            <BodyTemplate>
                <Textarea @bind-Value=WriteValue ShowLabel="true" ShowLabelTooltip="true" />
            </BodyTemplate>

        </PopConfirmButton>
    </RowButtonTemplate>

    <EditTemplate Context="context">
        <VariableEditComponent Model=@(context) AutoRestartThread="AutoRestartThread"></VariableEditComponent>
    </EditTemplate>

    <RowContentTemplate Context="context">


        @foreach (var col in context.Columns)
        {
            <td class="@GetFixedCellClassString(col, context)" style="@GetFixedCellStyleString(col, context)" @key=col>
                @{
                    var name = col.GetFieldName();
                    var data = VariableModelUtils.GetValue(context.Row, name);
                }
                <div class="@GetCellClassString(col, data, false, false)" @key=col>
                    @if (col.GetShowTips())
                    {
                        <Tooltip @key=col Title="@VariableModelUtils.GetValue(context.Row, name)" class="text-truncate d-block">
                            @data
                        </Tooltip>
                    }
                    else
                    {
                        @data
                    }
                </div>
            </td>
        }

    </RowContentTemplate>

    <ExportButtonDropdownTemplate Context="ExportContext">
        @if ((AuthorizeButton("导出")))
        {
            <Button IsAsync class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext, true)" IsKeepDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@RazorLocalizer["ExportAll"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelExportAsync(ExportContext)" IsKeepDisabled=@(!AuthorizeButton("导出"))>
                <i class="fas fa-file-export"></i>
                <span>@RazorLocalizer["TablesExportButtonExcelText"]</span>
            </Button>
        }
        @if ((AuthorizeButton("导入")))
        {
            <Button class="dropdown-item" OnClick="() => ExcelImportAsync(ExportContext)" IsKeepDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@RazorLocalizer["TablesImportButtonExcelText"]</span>
            </Button>
            <Button class="dropdown-item" OnClick="() => ExcelVariableAsync(ExportContext)" IsKeepDisabled=@(!AuthorizeButton("导入"))>
                <i class="fas fa-file-import"></i>
                <span>@GatewayLocalizer["ExcelVariable"]</span>
            </Button>
        }
    </ExportButtonDropdownTemplate>
    <TableToolbarTemplate>

        <TableToolbarButton TItem="VariableRuntime" IsDisabled=@(!AuthorizeButton(AdminOperConst.Add))
                            Color=Color.Success Text="@RazorLocalizer["Copy"]"
                            OnClickCallback=@(Copy) />

        <TableToolbarButton TItem="VariableRuntime" IsDisabled=@(!AuthorizeButton(AdminOperConst.Edit))
                            Color=Color.Info Text="@RazorLocalizer["BatchEdit"]"
                            OnClickCallback=@(BatchEdit) />

        <TableToolbarPopConfirmButton TItem="VariableRuntime"
                                      Color=Color.Warning Text="@RazorLocalizer["Clear"]" IsDisabled=@(!AuthorizeButton(AdminOperConst.Delete))
                                      IsAsync OnConfirm=@(ClearAsync) />

        @if (WebsiteOption.Value.Demo || App.HostEnvironment.IsDevelopment())
        {
            <PopConfirmButton Color=Color.Warning Text="@Localizer["Test"]" IsKeepDisabled=@(!AuthorizeButton(AdminOperConst.Add))
                              IsAsync OnConfirm=@(InsertTestDataAsync) class="me-1">

                <BodyTemplate>
                    <BootstrapInput @bind-Value=TestVariableCount ShowLabel="true" ShowLabelTooltip="true" />
                    <BootstrapInput @bind-Value=TestDeviceCount ShowLabel="true" ShowLabelTooltip="true" />
                    <Checkbox @bind-Value=BusinessEnable ShowLabel="true" ShowLabelTooltip="true" />
                </BodyTemplate>

            </PopConfirmButton>


            <PopConfirmButton Color=Color.Warning Text="@Localizer["TestDtu"]" IsKeepDisabled=@(!AuthorizeButton(AdminOperConst.Add))
                              IsAsync OnConfirm=@(InsertTestDtuDataAsync)>

                <BodyTemplate>
                    <BootstrapInput @bind-Value=TestDeviceCount ShowLabel="true" ShowLabelTooltip="true" />
                </BodyTemplate>

            </PopConfirmButton>
        }

    </TableToolbarTemplate>
</AdminTable>

@code {
    AdminTable<VariableRuntime> table;
}
